﻿//+-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description: NetworkProfileViewModel 
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using VmroleAuthor.Models;

namespace VmroleAuthor.ViewModels
{
    public class NetworkProfileViewModel : TreeViewItemViewModel, IDataErrorInfo, IDataWarningInfo, IProduceValidationResults, IAddNetworkAdapter, IRemoveChildItem
    {
        private readonly VmRoleResourceDefinitionViewModel vmRoleResourceDefinitionViewModel;
        private readonly VMNetworkProfile networkProfile;
        private readonly ObservableCollection<NetworkAdapterViewModel> networkAdapterViewModels;
       

        public static class PropertyNames
        {
            public const string Summary = "Summary";          
        }

        public NetworkProfileViewModel(
            VMNetworkProfile networkProfile,
            VmRoleResourceDefinitionViewModel resourceDefinitionViewModel)
            : base(resourceDefinitionViewModel, true)
        {
            if (networkProfile == null)
            {
                networkProfile = new VMNetworkProfile();
            }
            this.networkProfile = networkProfile;
            this.vmRoleResourceDefinitionViewModel = resourceDefinitionViewModel;
            this.networkAdapterViewModels = new ObservableCollection<NetworkAdapterViewModel>();
            if (this.networkProfile == null)
            {
                this.networkProfile = new VMNetworkProfile();
            }
            if (this.networkProfile.NetworkAdapters != null)
            {
                foreach (NetworkAdapter networkAdapter in this.networkProfile.NetworkAdapters)
                {
                    this.networkAdapterViewModels.Add(new NetworkAdapterViewModel(networkAdapter, this, resourceDefinitionViewModel));               
                }
            }          
            
            this.ValidationResults = new ObservableCollection<CloudServiceModelValidationResult>();         
        }

        public string Summary
        {
            get
            {                
                return "Network Profile";
            }
        }

        bool IAddNetworkAdapter.CanAddNetworkAdapter
        {
            get { return true; }
        }

        public void AddNetworkAdapter()
        {
            this.IsExpanded = true; //critical to be done first
            NetworkAdapter env = new NetworkAdapter();            
            env.IPAddresses = new List<IPAddress>();
            env.IPAddresses.Add(new IPAddress { AllocationMethod = IPAllocationMethod.Dynamic.ToString(), Type = IpProtocolVersion.IPV4.ToString(), ConfigurationName = "IPV4Configuration" });
            if (this.networkProfile.NetworkAdapters == null)
            {
                this.networkProfile.NetworkAdapters = new List<NetworkAdapter>();
            }
            env.Name = "NIC" + this.networkProfile.NetworkAdapters.Count + 1;
            this.networkProfile.NetworkAdapters.Add(env);
            var viewModel = new NetworkAdapterViewModel(env, this, this.vmRoleResourceDefinitionViewModel);
            this.networkAdapterViewModels.Add(viewModel);
            this.Children.Add(viewModel);
            viewModel.IsSelected = true;     
        }

        protected override void LoadChildren()
        {
            foreach (NetworkAdapterViewModel networkAdapterViewModel in networkAdapterViewModels)
            {
                this.Children.Add(networkAdapterViewModel);
            }          
        }

        public void RemoveChild(TreeViewItemViewModel childItem)
        {
            var networkAdapterViewModel = childItem as NetworkAdapterViewModel;
            if (networkAdapterViewModel != null)
            {
                networkAdapterViewModel.Cleanup();
                this.networkProfile.NetworkAdapters.Remove(networkAdapterViewModel.NetworkAdapter);               
                this.Children.Remove(childItem);              
            }    
        }    

        public string this[string propertyName]
        {
            get
            {
                return null;
            }
        }

        public string Error { get; set; }

        public ObservableCollection<CloudServiceModelValidationResult> ValidationResults { get; private set; }

        public void UpdateValidationResults()
        {
            this.IsExpanded = true;
            ModelValidationResultsHelper.UpdateValidationResults(this, typeof(PropertyNames));
            //Add any other cross-field validations error            
        }

        public string GetWarning(string propertyName)
        {
            if (this.networkProfile == null ||
                this.networkProfile.NetworkAdapters == null ||
                this.networkProfile.NetworkAdapters.Count == 0)
            {
                return "Add atleast one network adapter to network profile";
            }
            return null;
        }
    }
}
